<template>
	<view style="background: seagreen;height: 100%;">

		<fui-nav-bar fontWeight="bold" style="" background="transparent" color="#000" @rightClick="rightClick">
			<div
				style="border: 1px solid #eeeeee;;border-radius: 32rpx;padding: 6rpx;display: flex;flex-direction: row;width: 200rpx;justify-content: space-around;background-color: white;">
				<fui-icon style="margin: 0rpx 8rpx;" size="43" @click="leftClick" color="black"
					name="arrowleft"></fui-icon>
				<div class="link-column" style=""></div>
				<fui-icon style="margin: 0rpx 8rpx;" size="43" @click="gotoHome" color="black" name="home"></fui-icon>
			</div>
			<template v-slot:right>

			</template>
		</fui-nav-bar>

		<view
			style="box-sizing: border-box;padding: 48rpx 64rpx;display: flex;flex-direction: column;;height: 75%;background: white;position: absolute;bottom: 0;width: 100%;border-radius:36rpx 36rpx 0px 0px;">
			<view style="font-size: 48rpx;font-weight: 700;color: seagreen;">{{openId==null?'登录':'绑定'}}</view>

			<view
				style="background: rgb(248, 255, 255);display:flex;height: 96rpx;border-radius: 12rpx;margin-top: 48rpx;align-items: center;">
				<fui-icon color="seagreen" style="margin-left: 16rpx;" :size="48" name="my"></fui-icon>
				<fui-input backgroundColor="transparent" borderColor="transparent" :bottomLeft="0" borderTop
					placeholder="请输入用户名" v-model="userName"></fui-input>

			</view>

			<view
				style="background: rgb(242, 255, 253);display:flex;height: 96rpx;border-radius: 12rpx;margin-top: 32rpx;align-items: center;">
				<fui-icon color="seagreen" style="margin-left: 16rpx;" :size="48" name="lock"></fui-icon>
				<fui-input backgroundColor="transparent" borderColor="transparent" password :bottomLeft="0" borderTop
					placeholder="请输入密码" v-model="password"></fui-input>
			</view>

			<View style="display: flex;justify-content: space-between;margin-top: 32rpx;padding: 0rpx 16rpx;">
				<view style="font-size: 28rpx;color: black;">忘记密码？</view>
			</View>

			<view style="margin-top: 48rpx;">
				<fui-button @click="login" style="" background="#fff" color="seagreen"
					borderColor="seagreen">{{openId==null?'登录':'绑定'}}</fui-button>
			</view>
		</view>
	</view>
	<fui-toast ref="toast"></fui-toast>
</template>

<script>
	import request from "/request/request.js"
	export default {
		data() {
			return {
				userName: 'cgyztest',
				password: 'yt123456',
				openId: null,
			}
		},
		onLoad(options) {
			this.openId = options.openId;
			if (this.openId != null) {
				console.log("openId" + options.openId);
				uni.showToast({
					title: "请绑定账号"
				})
			}

		},
		methods: {
			gotoHome() {
				getApp().gotoHome();
			},
			leftClick() {
				uni.navigateBack();
			},
			login() {
				let that = this;
				if (this.username == '') {
					let options = {}
					//提示信息
					options.text = '请输入用户名';
					this.$refs.toast.show(options);
					return;
				}
				if (this.password == '') {
					let options = {}
					//提示信息
					options.text = '请输入密码';
					this.$refs.toast.show(options)
					return;
				}
				request({
					url: `/user/login`,
					method: 'POST',
					data: {
						userName: this.userName,
						password: this.password,
						openId: this.openId
					}
				}).then((res) => {
					if (res.code == 0) {
						res.data.password = this.password;
						let loginAccountListStr = uni.getStorageSync('loginAccountList');
						let loginAccountList = [];
						if (loginAccountListStr != null && loginAccountListStr != '') {
							loginAccountList = JSON.parse(loginAccountListStr);
						}
						console.log("this.deleteAccountById:");
						loginAccountList = this.deleteAccountById(loginAccountList, res.data.id);
						loginAccountList.push(res.data);
						uni.setStorageSync('loginAccountList', JSON.stringify(loginAccountList))
						console.log("loginAccountList:");
						console.log(loginAccountList + "");
						uni.switchTab({
							url: '/pages/home/home'
						})
						uni.setStorageSync('loginInfo', JSON.stringify(res.data))
					}
					let options = {}
					//提示信息
					options.text = res.userMsg;
					this.$refs.toast.show(options)
				}, (err) => {
					console.log('fail' + err);
				});
			},
			deleteAccountById(loginAccountList, id) {
				let newLoginAccountList = [];
				console.log("deleteAccountById");
				for (var i = 0; i < loginAccountList.length; i++) {
					if (loginAccountList[i].id != id) {
						console.log(loginAccountList[i].id + ";" + id);
						newLoginAccountList.push(loginAccountList[i]);
					}
				}
				return newLoginAccountList;
			},

			weixinLogin() {
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						// 登录成功
						uni.getUserInfo({
							provider: 'weixin',
							success: function(info) {
								console.log(info);
								// 获取用户信息成功, info.authResult保存用户信息
							}
						})
					},
					fail: function(err) {
						// 登录授权失败  
						// err.code是错误码
						console.log(err);
					}
				});
			},
		}
	}
</script>

<style>

</style>